<?php

namespace app\api\controller\admin;

use app\common\controller\Api;
use fast\Random;
use think\Db;

/**
 * 首页接口
 */
class Admin extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    public function index()
    {
        $this->success('请求成功66555');
    }

    /**
     * 查看
     */
    public function gindex()
    {


        $params = $this->request->param();

        $action =$params['action'];
        if($action=='apiindex'){
            $data= self::apiindex();
            return $data;
        }elseif ($action=='apiadd'){
            self::apiadd();
        }elseif ($action=='apiedit'){
            self::apiedit();
        }elseif ($action=='apidel'){
            self::apidel();
        }elseif ($action=='apilogin'){
            self::apilogin();
        }



    }

    /**
     * 查看
     */
    public function apiindex()
    {
        $params = $this->request->param();
        $pageSize =$params['pageSize'];
        $pageNum =$params['pageNum'];
        $name =isset($params['name']) ? $params['name'] : '';
        $sort=isset($params['sort']) ? $params['sort'] : 'id';
        $order=isset($params['order']) ? $params['order'] : 'desc';
        $systemFlag=isset($params['systemFlag']) ? $params['systemFlag'] : '';
        $where=[];
        if ($systemFlag){
            $where['id']  = array('like',  '%'.$systemFlag."%");
        }
        if ($name){
            $where['username']  = array('like',  '%'.$name."%");
        }

        $pageNum=($pageNum-1)*$pageSize;
        $list = Db::table('fa_g_admin')
            ->where($where)
            ->field(['password', 'salt', 'token'], true)
            ->order($sort, $order)
            ->limit($pageNum,$pageSize)
            ->select();
//        halt($list);
        $total= Db::table('fa_g_admin')
            ->where($where)
            ->field(['password', 'salt', 'token'], true)
            ->order($sort, $order)
            ->limit(1,2)
            ->count();
        foreach ($list as $k => &$v) {
            $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
            $v['groups'] = implode(',', array_keys($groups));
            $v['groups_text'] = implode(',', array_values($groups));
        }
        unset($v);
        $result = array("total" => $total, "rows" => $list);
        return json($result);

    }
    /**
     * add
     */
    public function apiadd()
    {
        $params = $this->request->param();
//        $post = $this->request->post();
//        halt($post);
        unset($params['action']);
        unset($params['/api/admin/admin/gindex']);
        $params['salt'] = Random::alnum();
        $params['password'] = md5(md5($params['password']) . $params['salt']);
        $params['avatar'] = isset($params['avatar']) ? $params['avatar'] : '/assets/img/avatar.png'; //设置新管理员默认头像。
        $params['createtime']=time();
        $list=Db::table('fa_g_admin')->insertGetId($params);

        $this->success($list);

    }
    /**
     * up
     */

    public function apiedit()
    {
        $params = $this->request->param();
        unset($params['action']);
        unset($params['/api/admin/admin/gindex']);
//        halt($params);
        $params['updatetime']=time();
        $list=Db::table('fa_g_admin')->update($params);
        $this->success($list);

    }
    public function apidel()
    {
        $params = $this->request->param();
        unset($params['action']);
        unset($params['/api/admin/admin/gindex']);
//        halt($params);
        $list=Db::table('fa_g_admin')->delete($params);
        $this->success($list);

    }
    public function apilogin()
    {
        $post = $this->request->post();
        unset($post['action']);
        $username = $this->request->post('username');
        $password = $this->request->post('password');
        $where['username']=$username;
        $listone=Db::table('fa_g_admin')->where($where)->find();
//    用户id查权限id
        $group=Db::table('fa_g_auth_group_access')->where('uid',$listone['id'])->find();
//        权限id查权限组
        $groupqx=Db::table('fa_g_auth_group')->where('id',$group['group_id'])->find();
//        如果是*就拥有全部权限
        if($groupqx['rules']=='*'){
            $rulesarr=Db::table('fa_g_auth_rule')->column('id');

        }
        $listone['qxrules']=$rulesarr;
        $password = md5(md5($password) . $listone['salt']);
//        halt($password);
        if ($listone){
        }else{
            $this->error('账号不存在');
        }
        if ($listone['password']!=$password){
            $this->error('密码错误');
        }
        $this->success('登录成功',$listone);
    }
}
